from selenium import webdriver
import pandas as pd

wb = webdriver.Chrome("chromedriver.exe")  # 谷歌浏览器打开
wb.get("******")  # 要分析的网页
try:
    wb.implicitly_wait(10)

    wb.find_element_by_link_text("部门资源").click()
    element = wb.find_element_by_class_name("list-item")
    html = wb.page_source
    tables = pd.read_html(html)
    print(tables[0])
    datalist = []
    wb.find_element_by_id("sjx").click()
    header = [i for i in wb.find_elements_by_css_selector('thead>tr>th') if i.text]  # 标题行列表
    rows = [i for i in wb.find_elements_by_css_selector('tbody>tr') if i.text]  # 所有内容
    for row in rows:  # 逐行获取
        datalist.append([i.txt for i in row.find_elements_by_css_selector('td') if i.text])
    df = pd.DataFrame(datalist, columns=header)

    df.to_excel("filename.xlsx", index=False)
except:
    wb.quit()
    print('失败')
